package ex.klmy.gov.module;

import ex.klmy.gov.bean.Service;
import ex.klmy.gov.bean.ServiceCategory;
import ex.klmy.gov.bean.ServiceDetails;
import org.nutz.dao.Cnd;
import org.nutz.dao.Condition;
import org.nutz.ioc.annotation.InjectName;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.Ok;
import org.nutz.mvc.annotation.Param;

import java.util.List;

/**
 * Created by xiaguoqiang on 16/11/3.
 */
@IocBean
@InjectName
@At("/zwfw")
public class ServerAction extends BaseAction{

    @At
    @Ok("jsonp")
    public Object getMainServiceCats() {
        List<ServiceCategory> l = basicDao.search(ServiceCategory.class, Cnd.wrap("where wbfcatcode = 'ROOT' and wblevel = 0"));
        return l;
    }

    @At
    @Ok("jsonp")
    public Object getServiceCatsByParentId(@Param("pCatCode")String id) {
        List<ServiceCategory> l = basicDao.search(ServiceCategory.class, Cnd.wrap("where wbfcatcode = '" + id + "'"));
        return l;
    }

    @At
    @Ok("jsonp")
    public Object getServicesByCat(@Param("category")String category) {
        List<Service> l = basicDao.search(Service.class, Cnd.wrap("where wbcatcode = '" + category + "'"));
        return l;
    }

    @At
    @Ok("jsonp")
    public Object getServicesByTreeId(@Param("tId")String tId, @Param("pageNo")int pageNo,@Param("pageSize") int pageSize) {
        String sql = "select [dbo].[wbeworkitem].*\n" +
                "from [dbo].[wbeworkitem] left join [dbo].[wbeworkcategory] on [dbo].[wbeworkitem].[wbcatcode] = [dbo].[wbeworkcategory].[wbcatcode] \n" +
                "left join [dbo].[wbtree] on [dbo].[wbeworkcategory].[wbcatname] =  [dbo].[wbtree].[wbname] \n" +
                "where [dbo].[wbtree].[wbtreeid] = '" + tId + "'";
        List<Service> l = basicDao.searchM(Service.class, sql,pageNo,pageSize);
        return l;
    }

    @At
    @Ok("jsonp")
    public Object getServicesByTreePid(@Param("tPid")String tPid, @Param("pageNo")int pageNo,@Param("pageSize") int pageSize) {
        String sql = "select [dbo].[wbeworkitem].*\n" +
                "from [dbo].[wbeworkitem] left join [dbo].[wbeworkcategory] on [dbo].[wbeworkitem].[wbcatcode] = [dbo].[wbeworkcategory].[wbcatcode] \n" +
                "left join [dbo].[wbtree] on [dbo].[wbeworkcategory].[wbcatname] =  [dbo].[wbtree].[wbname] \n" +
                "where [dbo].[wbtree].[wbtreefid] = '"  + tPid + "'";
        List<Service> l = basicDao.searchM(Service.class, sql,pageNo,pageSize);
        return l;
    }


    @At
    @Ok("jsonp")
    public Object getServiceById(@Param("id")String id) {
        ServiceDetails l = basicDao.findByCondition(ServiceDetails.class,Cnd.wrap("where wbeworkitemid = '" + id + "'"));
        return l;
    }
}
